<template>
  <div class="my-application-page">
    <el-card>
      <div class="header-title">我的申请</div>
      
      <!-- 统计信息 -->
      <div class="stats-container">
        <el-row :gutter="20">
          <el-col :span="6">
            <el-card shadow="hover" class="stat-card">
              <div class="stat-item">
                <div class="stat-number">{{ tableData.total }}</div>
                <div class="stat-label">申请总数</div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="6">
            <el-card shadow="hover" class="stat-card">
              <div class="stat-item">
                <div class="stat-number pending-count">{{ getStatusCount(0) }}</div>
                <div class="stat-label">审核中</div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="6">
            <el-card shadow="hover" class="stat-card">
              <div class="stat-item">
                <div class="stat-number approved-count">{{ getStatusCount(1) }}</div>
                <div class="stat-label">已通过</div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="6">
            <el-card shadow="hover" class="stat-card">
              <div class="stat-item">
                <div class="stat-number rejected-count">{{ getStatusCount(2) }}</div>
                <div class="stat-label">未通过</div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </div>
      
      <!-- 筛选表单 -->
      <el-form :inline="true" :model="filters" class="filter-form" @submit.prevent>
        <el-form-item label="岗位名称">
          <el-input v-model="filters.positionName" placeholder="输入岗位名称" clearable />
        </el-form-item>
        <el-form-item label="申请状态">
          <el-select v-model="filters.status" placeholder="全部状态" clearable style="width: 150px">
            <el-option label="审核中" value="0" />
            <el-option label="已通过" value="1" />
            <el-option label="未通过" value="2" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="fetchData">查询</el-button>
          <el-button @click="resetFilters">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 申请记录表格 -->
      <el-table 
        :data="tableData.records" 
        border 
        stripe 
        style="width: 100%; margin-top: 20px;"
        v-loading="loading"
        empty-text="暂无申请记录"
      >
        <el-table-column prop="positionName" label="岗位名称" align="center" min-width="120" />
        <el-table-column prop="description" label="工作内容" align="center" min-width="150" show-overflow-tooltip />
        <el-table-column prop="location" label="工作地点" align="center" min-width="100" />
        <el-table-column prop="salary" label="薪资(元/小时)" align="center" min-width="100" />
        <el-table-column prop="reason" label="申请原因" align="center" min-width="150" show-overflow-tooltip />
        <el-table-column prop="schedule" label="意向时间" align="center" min-width="120" />
        <el-table-column prop="status" label="申请状态" align="center" min-width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">
              {{ getStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="suggestion" label="审核意见" align="center" min-width="150" show-overflow-tooltip>
          <template #default="{ row }">
            <span v-if="row.suggestion">{{ row.suggestion }}</span>
            <span v-else class="text-muted">暂无</span>
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="申请时间" align="center" min-width="150">
          <template #default="{ row }">
            {{ formatDate(row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="120" fixed="right">
          <template #default="{ row }">
            <el-button 
              type="primary" 
              size="small" 
              @click="viewDetail(row)"
              :disabled="row.status === 0"
            >
              查看详情
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-wrapper">
        <el-pagination
          background
          layout="prev, pager, next, jumper, ->, total"
          :total="tableData.total"
          :page-size="filters.size"
          :current-page="filters.page"
          @current-change="handlePageChange"
        />
      </div>
    </el-card>

    <!-- 详情弹窗 -->
    <el-dialog v-model="detailDialogVisible" title="申请详情" width="600px">
      <el-descriptions :column="2" border>
        <el-descriptions-item label="岗位名称">{{ currentDetail.positionName }}</el-descriptions-item>
        <el-descriptions-item label="工作地点">{{ currentDetail.location }}</el-descriptions-item>
        <el-descriptions-item label="薪资标准">{{ currentDetail.salary }}元/小时</el-descriptions-item>
        <el-descriptions-item label="工作时间">{{ currentDetail.workTime }}</el-descriptions-item>
        <el-descriptions-item label="申请状态">
          <el-tag :type="getStatusType(currentDetail.status)">
            {{ getStatusText(currentDetail.status) }}
          </el-tag>
        </el-descriptions-item>
        <el-descriptions-item label="申请时间">{{ formatDate(currentDetail.createTime) }}</el-descriptions-item>
        <el-descriptions-item label="申请原因" :span="2">{{ currentDetail.reason }}</el-descriptions-item>
        <el-descriptions-item label="意向时间" :span="2">{{ currentDetail.schedule }}</el-descriptions-item>
        <el-descriptions-item label="审核意见" :span="2">
          <span v-if="currentDetail.suggestion">{{ currentDetail.suggestion }}</span>
          <span v-else class="text-muted">暂无审核意见</span>
        </el-descriptions-item>
        <el-descriptions-item label="工作内容" :span="2">{{ currentDetail.description }}</el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <el-button @click="detailDialogVisible = false">关闭</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getMyApplications } from '@/api/application'

// 筛选条件
const filters = ref({
  positionName: '',
  status: '',
  page: 1,
  size: 8
})

// 表格数据
const tableData = ref({
  records: [],
  total: 0
})

// 加载状态
const loading = ref(false)

// 详情弹窗
const detailDialogVisible = ref(false)
const currentDetail = ref({})

// 获取申请记录
const fetchData = async () => {
  loading.value = true
  try {
    // status转为数字或null
    const params = { ...filters.value, status: filters.value.status === '' ? null : Number(filters.value.status) }
    const res = await getMyApplications(params)
    if (res.code === 0) {
      tableData.value = {
        records: res.data.records || [],
        total: res.data.total || 0
      }
      filters.value.page = res.data.current || 1
      filters.value.size = res.data.size || 8
    } else {
      ElMessage.error(res.message || '获取申请记录失败')
    }
  } catch (error) {
    ElMessage.error('网络错误，获取申请记录失败')
  } finally {
    loading.value = false
  }
}

// 分页处理
const handlePageChange = (val) => {
  filters.value.page = val
  fetchData()
}

// 重置筛选条件
const resetFilters = () => {
  filters.value = {
    positionName: '',
    status: '',
    page: 1,
    size: 8
  }
  fetchData()
}

// 获取状态类型
const getStatusType = (status) => {
  switch (status) {
    case 0: return 'warning'  // 审核中
    case 1: return 'success'  // 已通过
    case 2: return 'danger'   // 未通过
    default: return 'info'
  }
}

// 获取状态文本
const getStatusText = (status) => {
  switch (status) {
    case 0: return '审核中'
    case 1: return '已通过'
    case 2: return '未通过'
    default: return '未知'
  }
}

// 获取状态统计
const getStatusCount = (status) => {
  if (!tableData.value.records) return 0
  return tableData.value.records.filter(item => item.status === status).length
}

// 格式化日期
const formatDate = (dateStr) => {
  if (!dateStr) return ''
  const date = new Date(dateStr)
  return date.toLocaleString('zh-CN', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit'
  })
}

// 查看详情
const viewDetail = (row) => {
  currentDetail.value = { ...row }
  detailDialogVisible.value = true
}

// 页面加载时获取数据
onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.my-application-page {
  padding: 24px;
}

.header-title {
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 20px;
  color: #222;
}

.filter-form {
  margin-bottom: 20px;
  padding: 16px;
  background-color: #f8f9fa;
  border-radius: 4px;
}

.pagination-wrapper {
  margin-top: 20px;
  text-align: right;
}

.text-muted {
  color: #999;
}

:deep(.el-table .el-table__cell) {
  padding: 8px 0;
}

:deep(.el-descriptions__label) {
  font-weight: 600;
}

.stats-container {
  margin-bottom: 20px;
}

.stat-card {
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.stat-item {
  text-align: center;
}

.stat-number {
  font-size: 24px;
  font-weight: bold;
  margin-bottom: 8px;
}

.pending-count {
  color: #e6a23c;
}

.approved-count {
  color: #67c23a;
}

.rejected-count {
  color: #f56c6c;
}

.stat-label {
  font-size: 14px;
  color: #999;
}
</style> 